﻿using HelixToolkit.Wpf.SharpDX.Elements2D;
using HomagChina.CCHCS.DataTypeModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;
using static HomagChina.CCHCS.ProcessResourceInternal;

namespace HomagChina.CCHCS
{
    public class LayerCreator
    {
        private const double RealLengthMm = 3000;
        private const double RealWidthMm = 1000;
        private const double LengthScale = 0.39; // X轴缩放因子
        private const double WidthScale = 0.37; // Y轴缩放因子

        public Canvas CanvasElement { get; set; }

        public LayerCreator(Canvas canvas)//Canvas canvas
        {
            //InitializeCanvas();
            CanvasElement = canvas;
            CanvasElement.Width = MmToXDip(RealLengthMm);
            CanvasElement.Height = MmToYDip(RealWidthMm);
            CanvasElement.Background = GetHexadecimalColor("#819cbe");
        }

        public void InitializeCanvas(Canvas canvas)
        {
            CanvasElement = canvas;
            CanvasElement.Width = MmToXDip(RealLengthMm);
            CanvasElement.Height = MmToYDip(RealWidthMm);
            CanvasElement.Background = GetHexadecimalColor("#819cbe");
        }

        internal void DrawLayers(List<Proxy.LayerInfo> layers)
        {
            CanvasElement.Children.Clear();

            if (layers == null || layers.Count == 0) return;

            AddPositionLabel(layers[0]);
            foreach (var layer in layers)
            {
                var rect = CreateRectangle(layer);
                CanvasElement.Children.Add(rect);
            }
        }

        private static double MmToXDip(double mm) => mm * LengthScale;

        private static double MmToYDip(double mm) => mm * WidthScale;

        private void AddPositionLabel(Proxy.LayerInfo layer)
        {
            var label = new Label
            {
                Name = layer.Name,
                Content = $"Position: {layer.RSShelfOrder}-{layer.RSLayerNum}",
                FontSize = 18,
                Opacity = 0.7,
                FontWeight = FontWeights.Bold,
                Foreground = GetHexadecimalColor("#F6C5A9")
            };
            Canvas.SetZIndex(label, 10);
            CanvasElement.Children.Add(label);
        }

        private Rectangle CreateRectangle(Proxy.LayerInfo layer)
        {
            var width = MmToXDip((double)(layer.Length * 0.75));
            var height = MmToYDip((double)layer.Width);

            //var centerX = MmToXDip((double)layer.CalCenterX) - width / 2;
            //var centerY = MmToYDip((double)layer.RSCenterY) - height / 2;

            var centerX = MmToXDip((double)layer.RSCenterX);
            var centerY = MmToYDip((double)layer.RSCenterY);

            var rectangle = new Rectangle
            {
                Width = width,
                Height = height,
                Fill = GetHexadecimalColor("#ff9300"),
                Stroke = GetHexadecimalColor("#000000"),
                StrokeThickness = 0.5,
                RadiusX = 2,
                RadiusY = 2,
                RenderTransform = new RotateTransform(layer.PackRt90 ? 90 : 0, width / 2, height / 2)
            };

            Canvas.SetLeft(rectangle, centerX);
            Canvas.SetTop(rectangle, centerY);

            return rectangle;
        }
    }
}